package com.thuan.android3d;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Triangle {

	private FloatBuffer vertices_Buffer;

	private ShortBuffer indices_Buffer;
	
	private FloatBuffer colors_buffer;

	
	private int abc;
	public Triangle() {
		initialize();		
	}

	public void initialize() {

		float vertices[] = { 
					-0.5f, -0.5f, 0.5f, // 0
		            0.5f, -0.5f, 0.5f, // 1
		            0f, -0.5f, -0.5f, // 2
		            0f, 0.5f, 0f, // 3
		};
		
		short indices[] = new short[]{
					0, 1, 3, // rwg
		            0, 2, 1, // rbg
		            0, 3, 2, // rbw
		            1, 2, 3, // bwg
		};
		
		 float[] colors = {
		            1f, 0f, 0f, 1f, // point 0 red
		            0f, 1f, 0f, 1f, // point 1 green
		            0f, 0f, 1f, 1f, // point 2 blue
		            1f, 1f, 1f, 1f, // point 3 white
		   };
		ByteBuffer bbb=ByteBuffer.allocateDirect(vertices.length*4);
		bbb.order(ByteOrder.nativeOrder());
		vertices_Buffer=bbb.asFloatBuffer();		
		vertices_Buffer.put(vertices);
		vertices_Buffer.position(0);
		
		
		ByteBuffer ccc=ByteBuffer.allocateDirect(indices.length*2);
		ccc.order(ByteOrder.nativeOrder());
		indices_Buffer=ccc.asShortBuffer();
		indices_Buffer.put(indices);
		indices_Buffer.position(0);
		
		ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
	    cbb.order(ByteOrder.nativeOrder());
	    colors_buffer= cbb.asFloatBuffer();
	    colors_buffer.put(colors);
	    colors_buffer.position(0);
	}

	public void onDrawGL(GL10 gl) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
				
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices_Buffer);
		
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colors_buffer);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, 12, GL10.GL_UNSIGNED_SHORT,indices_Buffer);
		
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
	
}
